//
//  HashTableQA.swift
//  Swift-LeetCode
//
//  Created by 卢悦明 on 2024/2/20.
//

import UIKit
/**
 swift 想要实现自定义的哈希表可以按照算法一期227之后开始写
 */
class HashTableQA: NSObject {
    
    class Student: Hashable {

        var age: Int
        var name: String
        var weight: Double
        init(age: Int, name: String, weight: Double) {
            self.age = age
            self.name = name
            self.weight = weight
        }
        
        static func == (lhs: HashTableQA.Student, rhs: HashTableQA.Student) -> Bool {
            
            return (lhs.age == rhs.age && lhs.name == rhs.name && lhs.weight == rhs.weight)
        }
        var hashValue: Int {
            get {
                var hashCode = age.hashValue
                hashCode = hashCode + name.hashValue
                hashCode = hashCode + weight.hashValue
                return hashCode
            }
        }
        func hash(into hasher: inout Hasher) {
        }
    }
    static func QA() {
        let str = "jack"
        var hashCode = 0
        let student = Student(age: 10, name: "lym", weight: 32.8)
        let student1 = Student(age: 10, name: "lym1", weight: 2.8)

//        print(student.hashValue, student1.hashValue)
        var dict = [Student: String]()
        dict[student] = "卢悦明"
        
        
        str.forEach { char in
            let code = char.asciiValue ?? 0
//            hashCode = hashCode * 31 + Int(code)
            hashCode = (hashCode << 5) - hashCode + Int(code)
        }
        print(hashCode)
    }

}
